package com.zhuge_new.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Adapter.PlanAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.PlanBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;

import java.lang.ref.PhantomReference;

/**
 * Created by lenovo on 2018-09-11.
 */

public class PlanActivity extends BaseActivity implements View.OnClickListener {

    private PlanAdapter adepter;
    private RecyclerView plan_list;
    private PlanAdapter planAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan);
        init_title();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title=findViewById(R.id.tv_nav_title);
        tv_title.setText(R.string.all_plan);
        ImageView iv_back=findViewById(R.id.iv_nav_back);
        iv_back.setOnClickListener(this);
        plan_list = findViewById(R.id.recycle_list);
        plan_list.setLayoutManager(new LinearLayoutManager(this));
        plan_list.setNestedScrollingEnabled(false);
        plan_datas();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(PlanActivity.class);
                break;
        }
    }
    private void plan_datas(){
        OkGo.<String>post(BaseHttp.repaymentplan_datas)
                .headers("token", PreferencesUtils.getString(baseContext,"token"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        final PlanBean planBean= (PlanBean) new DataParse().getBean(response.body(),PlanBean.class,0);
                        planAdapter = new PlanAdapter(PlanActivity.this,planBean);
                        plan_list.setAdapter(planAdapter);
                        planAdapter.setItemClickListener(new PlanAdapter.ItemClickListener() {
                            @Override
                            public void onItemClick(int position, int i) {
                                switch (i){
                                    case 11:
                                        Intent intent=new Intent(PlanActivity.this,PlandetailsActivity.class);
                                        intent.putExtra("repaymentplanId",planBean.getRepayments().get(position).getRepaymentplanId());
                                        intent.putExtra("RepaymentSum",planBean.getRepayments().get(position).getRepaymentSum());
                                        intent.putExtra("cardNo",planBean.getRepayments().get(position).getCardNo());
                                        startActivity(intent);
                                        break;
                                    case 12:
                                        delete(planBean.getRepayments().get(position).getRepaymentplanId(),position);
                                        break;
                                    case 21:
                                        Intent intent2=new Intent(PlanActivity.this,PlandetailsActivity.class);
                                        intent2.putExtra("repaymentplanId",planBean.getRepayments().get(position).getRepaymentplanId());
                                        intent2.putExtra("RepaymentSum",planBean.getRepayments().get(position).getRepaymentSum());
                                        intent2.putExtra("cardNo",planBean.getRepayments().get(position).getCardNo());
                                        startActivity(intent2);
                                        break;
                                    case 22:
                                        delete(planBean.getRepayments().get(position).getRepaymentplanId(),position);
                                        break;
                                }
                            }
                        });
                    }
                });
    }
    private void delete(final String repaymentplanId, final int position) {
        BounceTopEnter mBasIn = new BounceTopEnter();
        SlideBottomExit mBasOut = new SlideBottomExit();
        final NormalDialog dialog = new NormalDialog(baseContext);
        dialog.content("您确定要删除此计划吗")
                .style(NormalDialog.STYLE_TWO)
                .titleTextSize(23)
                .showAnim(mBasIn)
                .dismissAnim(mBasOut)
                .show();
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        execute_delete(repaymentplanId,position);
                        dialog.dismiss();
                    }
                });
    }

    private void execute_delete(String repaymentplanId, final int position) {
        OkGo.<String>post(BaseHttp.remove_repaymentplan)
                .tag(this)
                .headers("token",PreferencesUtils.getString(baseContext,"token"))
                .params("repaymentplanId",repaymentplanId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        MToast.makeTextShort(baseContext,msg).show();
                        planAdapter.removeData(position);

                    }
                });
    }
}
